package itsm.isperp.module.service.task;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.task.TskInspectCiCheck;
import itsm.isperp.module.repository.task.TskInspectCiCheckMapper;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2015-05-10 18:41
 */
@Service
public class TskInspectCiCheckService extends BaseService<TskInspectCiCheck> {

	@Autowired
	protected TskInspectCiCheckMapper tskInspectCiCheckMapper;

	@Override
	public TskInspectCiCheckMapper getRepository() {
		return this.tskInspectCiCheckMapper;
	}

	public List<Map<String, Object>> findByTaskIdAndCiId(String taskId,
			String ciId) {
		return this.tskInspectCiCheckMapper.findByTaskIdAndCiId(taskId, ciId);
	}

	public ResultMessage saveValues(HttpServletRequest req) {

		String[] ciCheckValues = req.getParameterValues("ciCheckValue");
		String[] ciCheckRemark = req.getParameterValues("ciCheckRemark");
		String[] ciCheckCiIds = req.getParameterValues("ciCheckCiId");
		String[] cateCheckIds = req.getParameterValues("cateCheckId");

		
		if(cateCheckIds==null||cateCheckIds.length==0){
			return ResultMessage.success();
		}
		
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String id = req.getParameter("id");

		jdbcTemplate.execute("DELETE FROM tsk_inspect_ci_check WHERE task_id='"
				+ id + "'");

		for (int i = 0; i < cateCheckIds.length; i++) {

			TskInspectCiCheck checks = new TskInspectCiCheck();
			checks.setTaskId(id);
			checks.setCiId(ciCheckCiIds[i]);
			checks.setCateCheckId(cateCheckIds[i]);
			checks.setCheckValue(ciCheckValues[i]);
			checks.setRemark(ciCheckRemark[i]);
			this.saveOrUpdate(checks);
		}

		return ResultMessage.success();
	}
}
